/**
 * Copyright 2011 Honeywell Federal Manufacturing & Technologies, LLC. This material was produced under U.S. Government
 * contract DE-ACO4-01AL66850, Honeywell Federal Manufacturing & Technologies, LLC, (FM&T) with the U. S. Department of
 * Energy for the operation of the Kansas City Plant . The U.S. Government has rights to use, reproduce, and distribute
 * this software.  NEITHER THE GOVERNMENT NOR Honeywell Federal Manufacturing & Technologies, LLC, MAKES ANY WARRANTY,
 * EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.  If software is modified to produce
 * derivative works, such modified software should be clearly marked, so as not to confuse it with the version available
 * from FM&T.
 *
 * Additionally, this program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License Version 3 published by the Free Software Foundation. Accordingly, this program is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this library.
 * If not, see http://www.gnu.org/licenses/.
 */
package org.braintrain.services.util.domContentWorkers;

import org.braintrain.domain.Navigation;
import org.braintrain.domain.ResourceReference;
import org.braintrain.services.util.DOMContentParser;
import org.braintrain.tapestry.encoders.ContentServiceEncoder;

public class ContentLinkValidator extends AbstractNullCheckingAttributeValidator  {

    private DOMContentParser domParser;

    public ContentLinkValidator(DOMContentParser domParser)
    {
        this.domParser = domParser;
    }
    
    /**
     * Determines if a the value of a node fits the 'Page Path' format, if the
     * linked page exists in the database:
     * 
     * Page Path format : '/Pages/...'
     * 
     * Also fixes corrupted links, basically making sure the current navigation links to the given navigation.
     * 
     * @param nodeValue
     */
    protected void processAttributeValue(String nodeValue)
    {
        boolean value = false;
            
        int pagesIndex = nodeValue.indexOf(ContentServiceEncoder.PAGE_PATH + "/");
        Navigation navigation = null;
        if(pagesIndex == 0)
        {
            Object[] params = domParser.getDataSqueezer().unsqueeze(ContentServiceEncoder.parsePagePath(nodeValue));
            
            if(params != null && params.length > 2 && params[1] instanceof Long && params[2] instanceof Long)   
            {
                navigation = domParser.getGenericDAO().getById(Navigation.class, (Long)params[1]);
            
        
                if(navigation != null)
                {
                    domParser.getReferences().add(new ResourceReference(navigation));
                    value = domParser.isAllowPageLinks();
                    
                    // parent page must be current page
                    if(!((Long)params[2]).equals(domParser.getNavigationId()) && value)
                    {
                        //fix the link
                        setNodeValue(domParser.generatePagePath(navigation));
                    }
                }
            }
        }
    }

}
